package com.weishenai.customerassistant.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weishenai.customerassistant.common.dto.SysMenuDto;
import com.weishenai.customerassistant.entity.sys.SysMenuEntity;
import com.weishenai.customerassistant.entity.sys.SysUserEntity;
import com.weishenai.customerassistant.mapper.SysMenuMapper;
import com.weishenai.customerassistant.mapper.SysUserMapper;
import com.weishenai.customerassistant.service.SysMenuService;
import com.weishenai.customerassistant.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author weishenai
 * @since 2021-11-27
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenuEntity> implements SysMenuService {
    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * @Description: 获取当前用户菜单导航
     * @Param: []
     * @return: java.util.List<com.weishenai.blog.common.dto.SysMenuDto>
     * @Author: weishenai
     * @Date: 2021/11/28
     */
    @Override
    public List<SysMenuDto> getCurrentUserNav() {
        String username = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        SysUserEntity sysUser = sysUserService.getByUsername(username);

        // 获取用户的所有菜单
        List<Long> menuIds = sysUserMapper.getNavMenuIds(sysUser.getId());
        List<SysMenuEntity> menus = buildTreeMenu(this.list(Wrappers.lambdaQuery(SysMenuEntity.class)
                .in(SysMenuEntity::getId,menuIds)
                .orderByAsc(SysMenuEntity::getOrdernum)));
        return convert(menus);
    }

    /**
     * @Description: 把list转换成树形结构数据
     * @Param: [listByIds]
     * @return: java.util.List<com.weishenai.blog.entity.sys.SysMenuEntity>
     * @Author: weishenai
     * @Date: 2021/11/28
     */
    private List<SysMenuEntity> buildTreeMenu(List<SysMenuEntity> menus) {
        List<SysMenuEntity> finalMenus = new ArrayList<>();
        for (SysMenuEntity menu : menus) {
            //先找到各自的孩子
            for (SysMenuEntity e : menus) {
                if (Objects.equals(e.getParentId(), menu.getId())) {
                    menu.getChildren().add(e);
                }
            }
            // 提取出父节点
            if (menu.getParentId() == 0L){
                finalMenus.add(menu);
            }
        }
        return finalMenus;
    }

    /**
     * @Description: menu 转 dto
     * @Param: [menus]
     * @return: java.util.List<com.weishenai.blog.entity.sys.SysMenuEntity>
     * @Author: weishenai
     * @Date: 2021/11/28
     */
    private List<SysMenuDto> convert(List<SysMenuEntity> menus) {
        List<SysMenuDto> menuDtos = new ArrayList<>();
        menus.forEach(m -> {
            SysMenuDto dto = new SysMenuDto();
            dto.setId(m.getId());
            dto.setName(m.getPerms());
            dto.setTitle(m.getName());
            dto.setComponent(m.getComponent());
            dto.setIcon(m.getIcon());
            dto.setPath(m.getPath());
            dto.setType(m.getType());
            if (m.getChildren().size() > 0) {
                dto.setChildren(convert(m.getChildren()));
            }
            menuDtos.add(dto);
        });
        return menuDtos;
    }
}
